//
//  CLBaseRequest.m
//  Charles
//
//  Created by Charles on 2018/6/6.
//  Copyright © 2018年 charles. All rights reserved.
//

#import "CLBaseRequest.h"
#import "CLNetWorkManager.h"

@interface CLBaseRequest()

@property (nonatomic, strong, readwrite) NSURLSessionTask *requestTask;
@property (nonatomic, strong, readwrite) NSData *responseData;
@property (nonatomic, strong, readwrite) id responseJSONObject;
@property (nonatomic, strong, readwrite) id responseObject;
@property (nonatomic, strong, readwrite) NSString *responseString;
@property (nonatomic, strong, readwrite) NSError *error;

@end

@implementation CLBaseRequest

#pragma mark - 请求和响应信息

- (NSHTTPURLResponse *)response {
    return (NSHTTPURLResponse *)self.requestTask.response;
}

- (NSInteger)responseStatusCode {
    return self.response.statusCode;
}

- (NSDictionary *)responseHeaders {
    return self.response.allHeaderFields;
}

- (NSURLRequest *)currentRequest {
    return self.requestTask.currentRequest;
}

- (NSURLRequest *)originalRequest {
    return self.requestTask.originalRequest;
}

- (BOOL)isCancelled {
    if (!self.requestTask) {
        return NO;
    }
    return self.requestTask.state == NSURLSessionTaskStateCanceling;
}

-(BOOL)isExecuting {
    if (!self.requestTask) {
        return NO;
    }
    return self.requestTask.state == NSURLSessionTaskStateRunning;
}

#pragma mark - 请求配置

- (void)setCompletionBlockWithSuccess:(CLRequestCompletionBlock)success
                              failure:(CLRequestCompletionBlock)failure{
    self.successCompletionBlock = success;
    self.failureCompletionBlock = failure;
}

- (void)clearCompletionBlock {
    self.successCompletionBlock = nil;
    self.failureCompletionBlock = nil;
}

- (void)addAccessory:(id<CLRequestAccessory>)accessory {
    if (!self.requestAccessories) {
        self.requestAccessories = [NSMutableArray array];
    }
    [self.requestAccessories addObject:accessory];
}

#pragma mark - 请求行为

- (void)start {
    [[CLNetWorkManager sharedManager]addRequest:self];
}

- (void)stop {
    self.delegate = nil;
    [[CLNetWorkManager sharedManager]cancelRequest:self];
}

- (void)startWithCompletionBlockWithSuccess:(CLRequestCompletionBlock)success
                                    failure:(CLRequestCompletionBlock)failure {
    [self setCompletionBlockWithSuccess:success failure:failure];
    [self start];
}

#pragma mark - 子类重载的方法

- (void)requestCompletePreprocessor {
    
}

- (void)requestCompleteFilter {
    
}

- (void)requestFailedPreprocessor {
    
}

- (void)requestFailedFilter {
    
}

- (NSString *)requestUrl{
    return @"";
}

- (NSString *)cdnUrl {
    return @"";
}

- (NSString *)baseUrl {
    return @"";
}

-(NSTimeInterval)requestTimeoutInterval {
    return 60;
}

- (id)requestArguments {
    return nil;
}

- (id)cacheFileNameFilterForRequestArgument:(id)argument {
    return argument;
}

- (CLRequestMethod)requestMethod {
    return CLRequestMethodGET;
}

- (CLRequestSerializerType)requestSerializerType {
    return CLRequestSerializerTypeHTTP;
}

- (CLResponseSerializerType)responseSerializerType {
    return CLResponseSerializerTypeJSON;
}

- (NSArray<NSString *> *)requestAuthorizationHeaderFieldArray {
    return nil;
}

- (NSDictionary<NSString *,NSString *> *)requestHeaderFieldValueDictionary {
    return nil;
}

- (NSURLRequest *)buildCustomUrlRequest {
    return nil;
}

- (BOOL)allowsCellularAccess {
    return YES;
}

- (id)jsonValidator {
    return nil;
}

- (NSString *)description {
    return [NSString stringWithFormat:@"<%@: %p>{ URL: %@ } { method: %@ } { arguments: %@ }", NSStringFromClass([self class]), self, self.currentRequest.URL, self.currentRequest.HTTPMethod, self.requestArguments];
}

@end
